package com.wm.web.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Collect;
import com.wm.bean.ResMenu;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.CollectService;
import com.wm.service.OrdersService;
import com.wm.service.ResMenuService;
import com.wm.service.RestaurantService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserSession;

public class ProfileAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(ProfileAction.class);
	
	private CollectService collectService;
	private UserService userService;
	private OrdersService ordersService;

	public OrdersService getOrdersService() {
		return ordersService;
	}

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CollectService getCollectService() {
		return collectService;
	}

	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}
	
	private Map session;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private User user;
	private String pwdshow;
	private int collectSize;
	private PageList pageList;

	public int getCollectSize() {
		return collectSize;
	}

	public void setCollectSize(int collectSize) {
		this.collectSize = collectSize;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getPwdshow() {
		return pwdshow;
	}

	public void setPwdshow(String pwdshow) {
		this.pwdshow = pwdshow;
	}

	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				return this.executeMethod(this.getAction());
			}
			return "default";
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		this.setUser(this.getUserService().findUserById(us.getUid()));
		this.getPwd(this.getUser().getUserPwdShow());
		//收藏
		List cl = this.getCollectService().findCollectByUserIdObjType(us.getUid(), 0);
		this.setCollectSize(cl.size());
		
		//订单
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/profile.action");
		this.setPageList(this.getOrdersService().findOrdersByUserIdFindType(us.getUid(),1,"addTime", 1, pages));
		return "index";
	}
	
	/**
	 * 密码安全级别
	 * @param value
	 */
	private void getPwd(String value){
		String str = "^[0-9]*$"; //数字
		String str2 ="^[A-Za-z]+$"; //字母
		String str3 = "^[A-Za-z0-9]+$"; //由字母、数字组成
		String str4 = "^[a-zA-Z0-9_]+$";//字母、数字、下划线
	    
		if(value.matches(str)){
			this.setPwdshow("0");
		}
		else if(value.matches(str2)){
			this.setPwdshow("0");
		}
		else if(value.matches(str3)){
			this.setPwdshow("1");
		}
		else if(value.matches(str4)){
			this.setPwdshow("2");
		}
	}
	
	public static void main(String[] args) {
		String passwordStr = "aaaaa1111";
		String str = "^[0-9]*$"; //不超过20位的数字组合
		String str2 ="^[A-Za-z]+$"; //由字母不超过20位
		String str3 = "^[A-Za-z0-9]+$"; //由字母、数字组成，不超过20位
		String str4 = "^[a-zA-Z0-9_]+$";
		if(passwordStr.matches(str)){
			System.out.println("1");
		}
		if(passwordStr.matches(str2)){
			System.out.println("2");
		}
		if(passwordStr.matches(str3)){
			System.out.println("3");
		}
		if(passwordStr.matches(str4)){
			System.out.println("4");
		}
		
	}
}
